package com.cusc.module_monitor_location.mvvm.ui;

import android.view.View;

import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.cusc.module_monitor_location.BR;
import com.cusc.module_monitor_location.R;
import com.cusc.module_monitor_location.adapter.NodeDepartmentAdapter;
import com.cusc.module_monitor_location.databinding.FragDepartmentBinding;
import com.cusc.module_monitor_location.entry.NodeBean;
import com.cusc.module_monitor_location.factory.LocationMonitorModuleFactory;
import com.cusc.module_monitor_location.mvvm.vm.LocationMonitorViewModel;
import com.tcgroup.phone.base_core.event.event_bus.BaseFragmentEvent;
import com.tcgroup.phone.base_core.framework.mvvm.fragment.BaseMvvmFragment;
import com.tcgroup.phone.base_core.utils.KLog;

/**
 * 作者: wangfei
 * 创建时期: 2020/4/9.
 * 包名: com.cusc.module_monitor_location.mvvm.ui
 * 类名: OSAndroidMvvm
 */
public class DepartmentFragment extends BaseMvvmFragment<FragDepartmentBinding, LocationMonitorViewModel> {

    private NodeDepartmentAdapter mNodeDepartmentAdapter;
    private NodeBean nodeBean;

    public static DepartmentFragment newInstance() {
        DepartmentFragment fragment = new DepartmentFragment();
        return fragment;
    }

    @Override
    public Class<LocationMonitorViewModel> onBindViewModel() {
        return LocationMonitorViewModel.class;
    }

    public LocationMonitorViewModel createViewModel(){
        return ViewModelProviders.of(getActivity(),onBindViewModelFactory()).get(onBindViewModel());
    }

    @Override
    public ViewModelProvider.Factory onBindViewModelFactory() {
        return LocationMonitorModuleFactory.getInstance(getActivity().getApplication());
    }

    @Override
    public void initViewObservable() {

        mViewModel.getNodeBeanListViewEvent().observe(this, nodeBeans -> {
            mNodeDepartmentAdapter.setList(nodeBeans);
        });

        mViewModel.getChildNodeBeanListViewEvent().observe(this,nodeBeans -> {
            mNodeDepartmentAdapter.nodeAddData(nodeBean,0,nodeBeans);
            KLog.i("TAG","大小"+mNodeDepartmentAdapter.getData().size());
        });
    }

    @Override
    public int onBindVariableId() {
        return BR.model;
    }

    @Override
    public int onBindLayout() {
        return R.layout.frag_department;
    }


    @Override
    public String getToolbarTitle() {
        return null;
    }

    @Override
    public void initView(View view) {
        mNodeDepartmentAdapter = new NodeDepartmentAdapter();
        mBinding.recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        mBinding.recyclerview.setAdapter(mNodeDepartmentAdapter);
    }

    @Override
    public void initData() {
        super.initData();
    }


    @Override
    public <BaseNode> void onEvent(BaseFragmentEvent<BaseNode> event) {
        super.onEvent(event);
        switch (event.code){
            case 110:
                nodeBean = (NodeBean) event.data;
                mViewModel.getDepartmentToBus(nodeBean.getId());
                break;
        }
    }
}